/**
 * 全站路由配置
 *
 * 建议:
 * 1. 代码中路由统一使用name属性跳转(不使用path属性)
 */
import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

// 开发环境不使用懒加载, 因为懒加载页面太多的话会造成webpack热更新太慢, 所以只有生产环境使用懒加载
const _import = require('./import-' + process.env.NODE_ENV)

const router = new Router({
  routes: [
    { path: '/404', component: _import('common/404'), name: '404', meta: { title: '404未找到' } },
    { path: '/login', component: _import('common/login'), name: 'login', meta: { title: '登录' } },
    { path: '/',
      component: _import('main'),
      name: 'main',
      redirect: {name: 'home'},
      meta: { title: '主入口整体布局' },
      children: [
        { path: '/home', component: _import('common/home'), name: 'home', meta: { title: '首页' } },
        // 系统设置路由模块
        { path: '/admin', component: _import('system/admin'), name: 'admin', meta: { title: '管理员列表' } },
        { path: '/role', component: _import('system/role'), name: 'role', meta: { title: '角色列表' } },
        { path: '/menu', component: _import('system/menu'), name: 'menu', meta: { title: '菜单列表' } },
        { path: '/log', component: _import('system/log'), name: 'log', meta: { title: '日志列表' } },
        { path: '/theme', component: _import('common/theme'), name: 'theme', meta: { title: '主题' } },

        { path: '/ueditor', component: _import('component/ueditor'), name: 'ueditor', meta: { title: '百度编辑器' } }

      ]
    }
  ]
})

// vue  路由全局拦截器
router.beforeEach(function (to, from, next) {
  let token = sessionStorage.getItem('token')
  if (to.path !== '/login') {
    if (!token) {
      next({ name: 'login' }) // 没有token 跳转登录页面
    }
  }
  next()
})

export default router
